#include "StdMyString.h"
#include<cstring>
StdMyString::StdMyString():capacity(15),size(0)
{
    string = new char[capacity];
}

StdMyString::StdMyString(const char *str)
{
    size = strlen(str);
    capacity = size+1;
    string = new char[capacity];
    strcpy(string, str);
}

StdMyString::StdMyString(const StdMyString &str)
{
    size = str.size;
    capacity = str.capacity;
    string = new char[capacity];
    strcpy(string, str.string);
}

StdMyString::~StdMyString() 
{
    delete[] string;
}

StdMyString& StdMyString::operator=(const StdMyString &str)
{
    
    if(capacity<=str.size)
    {
        capacity = str.capacity;
        delete[] string;
        string = new char[capacity];
    }
    size = str.size;
    strcpy(string, str.string);
    return *this;
}

StdMyString &StdMyString::operator+=(const StdMyString &str)
{
    size += str.size;
    if(capacity<=size)
    {  
        capacity = size + 1;
        char *newP= new char[capacity];
        strcpy(newP, string);
        delete[]string;
        string = newP;
    }
    strcat(string, str.string);
    return *this;
}

StdMyString StdMyString::operator+(const StdMyString &str)
{
    StdMyString result = *this;
    result += str;
    return result;
}

StdMyString& StdMyString::operator-=(const StdMyString &str)
{
    char *dest = strstr(string, str.string);
    while(dest!=nullptr)
    {
        char *back = dest + str.size;
        while(*back!='\0')
            *dest++ = *back++;
        *dest = '\0';
        dest = strstr(string, str.string);
    }
    return *this;
}

StdMyString StdMyString::operator-(const StdMyString &str)
{
    StdMyString result = *this;
    result -= str;
    return result;
}

StdMyString &StdMyString::operator+=(const char &c)
{
    if (capacity <= size)
    {  
        capacity *= 2;
        char *newP= new char[capacity];
        strcpy(newP, string);
        delete[]string;
        string = newP;
    }
    string[size++] = c;
    string[size] = '\0';
    return *this;
}

char &StdMyString::operator[](int index)
{
    return string[index];
}

bool StdMyString::operator==(const StdMyString &str)
{
    return strcmp(string, str.string)==0;
    
}

bool StdMyString::operator>(const StdMyString &str)
{
    return strcmp(string, str.string)>0;
}

bool StdMyString::operator<(const StdMyString &str)
{
    return strcmp(string, str.string)<0;
}

std::ostream &operator<<(std::ostream &os, const StdMyString &str)
{
    os << str.string<<std::endl;
    return os;
}

std::istream &operator>>(std::istream &is, StdMyString &str)
{
    str.size = 0;
    char temp = 0;
    while((temp=getchar())!='\n')// 从终端获取一个字符
    {
        str += temp;
    }
    return is;
}


StdMyString::operator int()//将字符串转为整型
{
    return atoi(string);
}

StdMyString::operator double() //将字符串转为浮点型
{
    return atof(string);
}

MyStringList StdMyString::split(const StdMyString &str)
{
    StdMyString CPStr = *this;
    MyStringList list;
    char *dest = strtok(CPStr.string, str.string);
    while(dest!=nullptr)
    {
        list += dest;
        dest = strtok(nullptr, str.string);
    }
    return list;
}

MyStringList::MyStringList(int capacity):size(0),capacity(capacity)
{
    array = new StdMyString[capacity];
}

MyStringList &MyStringList::operator+=(const StdMyString &str)
{
    if(capacity<=size)
    {
        capacity *= 2;
        StdMyString *newP = new StdMyString[capacity];
        for (int i = 0; i < size;i++)
        {
            newP[i] = array[i];
        }
        delete[] array;
        array = newP;
    }
    array[size++] = str;
    return *this;
}
MyStringList::~MyStringList() 
{
    delete[] array;
}
MyStringList &MyStringList::operator<<(const StdMyString &str)
{
    *this+=str;
    return *this;
}
MyStringList MyStringList::operator+(const MyStringList &list)
{
    MyStringList result;
    for (int i = 0; i < size;i++)
    {
        result += array[i];
    }
    for (int i = 0; i < list.size;i++)
    {
        result += list.array[i];
    }
        return result;
}
std::ostream &operator<<(std::ostream &os, const MyStringList &list)
{
    for (int i = 0; i < list.size; i++)
    {
        os << list.array[i];
    }
    return os;
}

MyStringList::MyStringList(const MyStringList &list)
    {
        size = list.size;
        capacity = list.capacity;
        array = new StdMyString[capacity];  // 分配新的数组空间
        
        for (int i = 0; i < size; i++) {
            array[i] = list.array[i];  // 深拷贝每个字符串
        }
    }

MyStringList &MyStringList::operator=(const MyStringList &list)
    {
    if(capacity<=list.size)
    {
        capacity = list.capacity;
        delete[] array;
        array= new StdMyString[capacity];
    }
    size = list.size;
    for (int i = 0; i < size; i++) {
        array[i] = list.array[i];  // 深拷贝每个字符串
    }
    return *this;
    }

    // MyStringList &MyStringList::operator-=(const StdMyString &str)
    // {
    //     for (int i = 0; i < size;i++)
    //     {
    //         if(array[i]==)
    //     }
    //         return *this;
    // }
